/**
 * Created by Administrator on 2018/11/7.
 */
function initAddSuplierView($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http, $cookies, $routeParams, $q, fileListService) {
    ;
    $$scope = $scope;
    $CertusFormAjax = CertusFormAjax;
    $CertusJsonAjax = CertusJsonAjax;
    $fileListService =fileListService;
    $CertusFormAjax.ajax("purchaseBaseInfoByBrCode", {
        "brCode":$routeParams.brCode
    }, function (res) {
        if (res.data) {
            $$scope.submitter = {};
            $$scope.submitter.purchaseComparisionStepEntity = {
                // "processInstanceId": $CertusJsonAjax.getProInstanceId(),
                // "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
            }
            $$scope.submitter.purchaseComparisionStepEntity.buyingRequisitionCode = $routeParams.brCode;

            var datas = res.data.buyings;

            var priceOption1 = {
                lang: 'zh-cn',
                ajaxLoad: false,
                extraWidth: 20,
                // loadURL:'url',
                exportFileName: '采购单条目信息',
                datas: datas,
                columns: proc1Columns,
                gridContainer: 'purchaselistContainer',
            };
            aboveGrid = $.fn.dlshouwen.grid.init(priceOption1);
            aboveGrid.load();
            if (datas) {
                for (var i in datas) {
                    if (datas[i].biCode) {
                        bqArray[datas[i].biCode] = new Array();
                    }
                }

            }

            var pricelogOption = {
                lang: 'zh-cn',
                ajaxLoad: false,
                extraWidth: 20,
                // loadURL:'url',
                exportFileName: '历史报价库',
                datas: [],
                columns: pricelogColumns,
                gridContainer: 'prcLogContainer',
                tools: '',
                pageSize: 10,
                pageSizeLimit: [10, 15, 20]
            };
            middleGrid = $.fn.dlshouwen.grid.init(pricelogOption);
            middleGrid.load();

            var prcListOption = {
                lang: 'zh-cn',
                ajaxLoad: false,
                extraWidth: 20,
                // loadURL:'url',
                exportFileName: '资产信息',
                datas: [],
                columns: priceColumnsAdd,
                gridContainer: 'priceContainer',
                // toolbarContainer: 'priceToolBarContainer',
                tools: '',
                pageSize: 10,
                pageSizeLimit: [10, 15, 20]
            };
            belowGrid = $.fn.dlshouwen.grid.init(prcListOption);
            belowGrid.load();
        }
    });

    $$scope.submmitData = {};
    // $$scope.submmitData.purchaseStepEntity = {
    //     "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    //     "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
    // }
    $$scope.select = {};
    $$scope.submitQuote = function () {

        if (certusValidateNotnull()) {
            $$scope.submitter.buyingQuoteItemEntities = bqArray;
            if ($(".shuplod").css("display") != "none") {
                layer.msg("有文件，还未提交");
                return;
            }
            if ($(".shpriceox").css("display") != "none") {
                layer.msg("有供应商信息，还没有添加完成");
                return;
            }
            $CertusJsonAjax.ajax("submitterProjQGquote", $$scope.submitter, function (res) {
                if (res.data) {
                    if (res.data == "success") {
                        findBacklogPerson($CertusFormAjax, $CertusJsonAjax.getStepInstanceId());
                        window.history.back();
                    }
                    layer.msg(res.data);
                }
            })
        }
    };
    $$scope.saveQuote = function () {
        if (certusValidateNotnullPolyfill()) {
            var biCode = $$scope.buyingQuoteItem.biCode;
            ;
            if (biCode) {
                //已有的缓存数组
                var bqArray2 = bqArray[biCode];
                var alreadyHas = false;
                if (bqArray2) {
                    for (var i in bqArray2) {
                        if (bqArray2[i].bqCode == $$scope.buyingQuoteItem.bqCode) {
                            bqArray2[i] = $$scope.buyingQuoteItem
                            alreadyHas = true;
                        }
                    }
                }
                if (alreadyHas == true) {
                    belowGrid.exhibitDatas = bqArray2;
                } else {
                    bqArray2.push($$scope.buyingQuoteItem);
                    belowGrid.exhibitDatas = bqArray2;
                }
                belowGrid.refresh(false);
                $(".shpriceox").hide();
            } else {
                layer.msg("没有bicode");
            }
        }
    }
    //上传文件
    $$scope.uploadBqFile = function (a, b) {
        fileUpload($$scope, function (res) {
            if (res.data) {
                //存入临时 变量中
                if (bqArray) {
                    if (bqArray[b]) {
                        var bqArray2 = bqArray[b];
                        for (var i in bqArray2) {
                            if (bqArray2[i]) {
                                if (bqArray2[i].bqCode == a) {
                                    if (!bqArray2[i].filePathArray) {
                                        bqArray2[i].filePathArray = new Array();
                                    }
                                    var da = {
                                        "fileId": res.data,
                                        "fileName": $$scope.file.fileName,
                                        "createTime": new Date()
                                    }
                                    bqArray2[i].filePathArray.push(da);
                                    layer.msg("文件上传成功");
                                    $(".shuplod").hide();
                                    return;
                                }
                            }
                        }
                    }
                }
            }
            layer.msg("文件上传失败");
        }, null, $CertusFormAjax);
    }
    //调用初始化方法
    initFileUpload($$scope, fileInstantiation, fileInstantiationDisable, $CertusFormAjax);
}


function findAddSuplierView($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http, $cookies, $routeParams, $q, fileListService){

    $$scope = $scope;
    $CertusFormAjax = CertusFormAjax;
    $CertusJsonAjax = CertusJsonAjax;
    $fileListService =fileListService;


    var dataInit = $CertusJsonAjax.dataInit("searchByPurchaseNo",{statProcess:"YES"},supplierColumns,'supplierContainer','supplierToolBarContainer');

    $$scope.stateChange = function(a){
        var id = $("#purchaseNoId").val();
        dataInit.parameters = {
            purchaseNoId : id,
            statProcess:$$scope.statProcess
        }
        dataInit.refresh(true);
    }

    $$scope.select = [
        {name:'已完成',code:"YES"},
        {name:'未完成',code:"NO"}
    ];

    $$scope.statProcess = "YES"

    $$scope.searchByPurchaseNo = function(){
        var id = $("#purchaseNoId").val();

        dataInit.parameters = {
            purchaseNoId : id,
            statProcess:$$scope.statProcess
        }
        dataInit.refresh(true);
    }


}

function findAddSuplierViewExame($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http, $cookies, $routeParams, $q, fileListService){

    $$scope = $scope;
    $CertusFormAjax = CertusFormAjax;
    $CertusJsonAjax = CertusJsonAjax;
    $fileListService =fileListService;

    $CertusFormAjax.ajax("comparisionExamineViewByPcs", {
        pcsCode : $routeParams.pcsCode
    }, function (res) {
        if (res.data) {

            // $$scope.headerMap = res.data.headerMap;
            $$scope.submmitData = {};
            $$scope.submmitData.purchaseComparisionStepEntity = {};
            $$scope.submmitData.purchaseComparisionStepEntity.buyingRequisitionCode = res.data.headerMap.buyingRequisitionCode;
            $$scope.submmitData.purchaseComparisionStepEntity.purchasingMsgDis = res.data.headerMap.purchasingMsg;

            var datas = res.data.buyings;
            $$scope.processs = res.data.processs;
            var priceOption1 = {
                lang: 'zh-cn',
                ajaxLoad: false,
                extraWidth: 20,
                exportFileName: '采购单条目信息',
                datas: datas,
                columns: proc1Columns,
                gridContainer: 'plistogContainer'
            };
            aboveGrid = $.fn.dlshouwen.grid.init(priceOption1);
            aboveGrid.load();

            //初始化 报价 文件 数据
            if (datas) {
                for (var i in datas) {
                    if (datas[i].biCode) {
                        bqArray[datas[i].biCode] = datas[i].buyingQuoteItemEntities;
                    }
                }

                var tempArray = new Array();
                if (bqArray) {
                    for (var j in bqArray) {
                        tempArray = tempArray.concat(bqArray[j]);
                    }
                }

                $$scope.items = res.data.buyings;
                $$scope.buying = res.data.buying;
                $$scope.allQuote = tempArray;
                $$scope.otherMap = res.data.otherMap;
                $$scope.headerMap = res.data.headerMap;
            }

            var prcListOption = {
                lang: 'zh-cn',
                ajaxLoad: false,
                extraWidth: 20,
                // loadURL:'url',
                exportFileName: '资产信息',
                datas: [],
                columns: pryslColumns,
                gridContainer: 'pricelogContainer',
                tools: '',
                pageSize: 10,
                pageSizeLimit: [10, 15, 20]
            };
            belowGrid = $.fn.dlshouwen.grid.init(prcListOption);
            belowGrid.load();

            showAllQuote();
        }
    });

    $$scope.getsssbbb = function(bb){
        ;
        //starting denied sentback others completed
        if(bb == "starting"){
            return "正在进行中";
        }else if(bb == "denied"){
            return "否决";
        }else if(bb == "sentback"){
            return "被退";
        }else if(bb == "others"){
            return "关闭";
        }else if(bb == "completed"){
            return "完成";    }else if(bb == "terminated"){        return "终结";    }    else if(bb == "cancelled"){        return "通过";}
        return "待办";
    }

    $$scope.admitAcceptance = function (bqCode, biCode) {

        if(certusValidateNotnullPolyfill()){
            if (bqCode && biCode) {
                $$scope.sbsb.bqCode = bqCode
                $$scope.sbsb.biCode = biCode;
                var bqArray2 = bqArray[biCode];
                if (bqArray2) {
                    for (var i in bqArray2) {
                        if (bqArray2[i].bqCode == bqCode) {
                            bqArray2[i].recievedNum = $$scope.acceptance.recievedNum
                            bqArray2[i].unit = $$scope.acceptance.unit
                            bqArray2[i].acceptanceResolve = $$scope.acceptance.acceptanceResolve
                            bqArray2[i].itAcceptance = $$scope.acceptance.itAcceptance
                        }
                    }

                }
                belowGrid.exhibitDatas = bqArray[biCode];
                belowGrid.refresh(false);
            }
            $("#myModal").modal('hide');
        }
    }

    $$scope.approvalAgreementSubmit = function () {

        if (certusValidateNotnull()) {
            $$scope.submmitData.purchaseStepEntity.findWhoType = 'C';
            $$scope.submmitData.buyingQuoteItemEntities = bqArray;
            $CertusJsonAjax.ajax("confirmPrintPurchase", $$scope.submmitData, function (res) {
                if (res.data) {
                    if (res.data == "success") {
                        findBacklogPerson($CertusFormAjax,$CertusJsonAjax.getStepInstanceId());
                        window.history.back();
                        return;
                    }
                    layer.msg(res.data);
                }
            })
        }
    }


}